home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: destructots and VC++
- Date: Fri, 19 Jan 1996 22:11:30 GMT
- Organization: Netcom
- Message-ID: <31001304.78037184@nntp.ix.netcom.com>
- References: <4doem9$7a8@zephyr.ens.tek.com> <30FFEDF3.2BC9@bangate.compaq.com>
- NNTP-Posting-Host: ix-dc6-09.ix.netcom.com
- X-NETCOM-Date: Fri Jan 19 2:11:29 PM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- Saurabh Dixit <saurabhd@bangate.compaq.com> wrote:
-
- > Err...
- > I wasn't even aware that constructors/destructors could
- > be invoked directly on objects from outside.
- > In fact I remember a few years back when I was in school
- > I tried to call the constructor on one of my objects to
- > re-initialize it and when my gnu++ croaked someone responded
- > to my query in this newsgroup to set me straight.
- >
- > Regarding VC++ I know the following works in 4.0
- >
- > class A {
- > int a;
- > int b;
- > public:
- > A (int _a)
- > {
- > a = 4;
- > A ();
- > }
- > A ()
- > {
- > b = 4;
- > };
- > };
- >
- > So from within the class, calling a constructor seems to work.
- > So maybe I'm thinking constructors and destructors can be
- > explicitly invoked from within the class but not from outside?
- > Anyone care to comment?
-
- This will work on any conforming C++ compiler in that it is legal code
- and the semantics are well defined. However, it is very unlikely that
- it is doing what you want.
-
- A() never calls the constructor for an existing object. It creates a
- temporary, constructs it using the default constructor, and destroys
- it. In your example, if you write
-
- A x(1);
-
- will construct x with x.a initialized to 4 and x.b uninitialized (or
- initialized to 0 if it is statically allocated). It will also
- construct a temporary A object with x.a uninitialized and x.b
- initialized to 4 and will destroy that object.
-
- >
- > Of course, I may be full of .... since the original poster
- > claims calling a destructor explicitly from outside is
- > supposed to work as it is the standard.
-
- The rules for a destructor are different. It is possible to call the
- destructor explicitly for an object.
-
-
- Michael M Rubenstein
-